package com.xiaomi.front.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiaomi.front.service.commoditys.CommService;
import com.xiaomi.front.service.malltype.MalltypeService;
import com.xiaomi.pojo.Commoditys;
import com.xiaomi.pojo.Malltype;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class SearchController {

    @Autowired
    private CommService commService;
    @Autowired
    private MalltypeService malltypeService;

    //跳转到搜索页，进行模糊查询/精准查询
    @GetMapping("/Search_name")
    public String GoSearch(@RequestParam("search") String name, @RequestParam(defaultValue = "0") int id, @RequestParam(defaultValue = "1") int currentPageNo, Model model) {
        //获取商品类型
        List<Malltype> malltypes = malltypeService.MalltypeAll();
        model.addAttribute("malltypes", malltypes);

        //设置当前页码和几条数据
        PageHelper.startPage(currentPageNo, 12);
        List<Commoditys> commoditys = commService.SearchComm(name, id);
        PageInfo<Commoditys> page = new PageInfo<Commoditys>(commoditys);
        model.addAttribute("page", page);

        //把搜索参数传到搜索页
        model.addAttribute("name", name);
        return "front/search";
    }

    //ajax实现分页查询，多表联查
    @RequestMapping(value = "/Ajax_Search", produces = {"json/application;charset=UTF-8"})
    @ResponseBody
    public Object Search(@RequestParam("search") String name, @RequestParam(defaultValue = "0") int id, @RequestParam(defaultValue = "1") int currentPageNo, Model model) {

        //设置当前页码和几条数据
        PageHelper.startPage(currentPageNo, 12);
        List<Commoditys> commoditys = commService.SearchComm(name, id);
        PageInfo<Commoditys> page = new PageInfo<Commoditys>(commoditys);
        model.addAttribute("page", page);

        //把搜索参数传到搜索页
        model.addAttribute("name", name);
        Map result = new HashMap<>();
        result.put("name", name);
        result.put("page", page);
        String json = JSON.toJSONString(result);
        return json;
    }

    //功能性查询
    @RequestMapping(value = "/TypeSearch", produces = {"json/application;charset=UTF-8"})
    @ResponseBody
    public Object TypeSearch(@RequestParam("search") String name, @RequestParam(defaultValue = "0") int id, int typeid, Model model) {

        model.addAttribute("name", name);

        List<Commoditys> commoditysList = commService.TypeSearch(name, id, typeid);
        model.addAttribute("commoditysList", commoditysList);

        String json = JSON.toJSONString(commoditysList);
        return json;

    }
}
